//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_name=thisform.find('input[name=goods_name]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		var manua_id=thisform.find('input[name=manua_id]').val();
		$('#booksInfoDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#booksInfoDatagrid').datagrid({ 
			queryParams:{
				manua_id:manua_id,
				goods_name:goods_name,
				goods_no:goods_no
			}
		});
	})
	
	//更新行数据
	function booksInfoUpdateActions(value){
		$.post(booksInfoEditUrl,{
									id			:value.id,
									goods_name	:value.goods_name,
									goods_no	:value.goods_no,
									goods_spec	:value.goods_spec,
									unit1		:value.unit1,
									unit2		:value.unit2,
									current_type:value.current_type,
									source		:value.source,
									source_no	:value.source_no,
									manua_id	:value.manua_id,
									item_no		:value.item_no,
									item_type	:value.item_type,
									hs_code		:value.hs_code
								},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#booksInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#booksInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#booksInfoDatagrid').datagrid('selectRow', index);
				$('#booksInfoDatagrid').datagrid('endEdit', editIndex);
				$('#booksInfoDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#booksInfoDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#booksInfoDatagrid').datagrid({
		//url:getarrivalUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getbooksInfoUrl,
		title:'账册详情',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'manua_id',title:'账册编号',sortable:true},
		{field:'goods_no',title:'商品编码',sortable:true,
				editor: {
					type: 'numberbox',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'goods_name',title:'商品名称',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'source_no',title:'料号',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'goods_spec',title:'规格',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},{field:'unit1',title:'第一单位',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getUnitUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.unit_name;
					var json = unit_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].unit_no){
							return json[i].unit_name;
						}
					}
				},
				editor: {
					type:'combobox',
					options:{
						valueField:'unit_no',
						textField:'unit_name',
						method:'get',
						url:UnitlistUrl,
						required:true,
						validType:['selected[]']
					}  
				}
		},{field:'unit2',title:'第二单位',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getUnitUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.unit_name;
					var json = unit_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].unit_no){
							return json[i].unit_name;
						}
					}
				},
				editor: {
					type:'combobox',
					options:{
						valueField:'unit_no',
						textField:'unit_name',
						method:'get',
						url:UnitlistUrl,
						required:false,
						validType:['selected[]']
					}  
				}
		},{field:'item_no',title:'项号',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},{field:'item_type',title:'料件性质',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},{field:'hs_code',title:'hs_code',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},{field:'current_type',title:'币制',width:'100px',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getCoinUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.coin_name;
					var json = coin_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].coin_code){
							return json[i].coin_name;
						}
					}
				},
				editor:{
					type:'combobox',
					options:{
						valueField:'coin_code',
						textField:'coin_name',
						method:'get',
						url:CoinlistUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'source',title:'产销国',width:'80px',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getCountryUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.country_name;
					var json = country_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].country_code){
							return json[i].country_name;
						}
					}
				},
				editor:{
					type:'combobox',
					options:{
						valueField:'country_code',
						textField:'country_name',
						method:'get',
						url:CountrylistUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'update_time',title:'更新时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true},
		{field:'status',title:'审核状态',sortable:true}
		]],
		queryParams:{
			cate_id:cateId
		},
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					booksInfoUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
booksInfoObj={
	add:function(url){//新增数据
		$('#booksInfoAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if(!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#booksInfoAddDialog').dialog('close');  
                    });
                }else{
					$.messager.alert('提示信息',result.message,'info',function(r){
                    	$('#booksInfoAddDialog').dialog('close');       
						$('#booksInfoDatagrid').datagrid('reload'); 
                    });
                }
            },
		});
	},
	save:function(url){//编辑后保存数据
		$('#booksInfoEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#booksInfoEditDialog').dialog('close');
                    });
                }else{
                    $('#booksInfoEditDialog').dialog('close');      
                    $('#booksInfoDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#booksInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#booksInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#booksInfoDatagrid').datagrid('acceptChanges');
		}
	},
	showAll:function(){//显示默认条件下所有数据
		$('#booksInfoDatagrid').datagrid({ queryParams:''});
	},
	/* infoBar:function(){//显示详情
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
        if (booksInfoRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(booksInfoRow.length==1){
			var id=booksInfoRow[0]['id'];
        	showTab(booksInfoUrl,'账册详情',id);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}, */
	reloadBar:function(){//刷新数据
		$('#booksInfoDatagrid').datagrid('reload');
	},
	recycle:function(){//查看回收站数据
		$('#booksInfoDatagrid').datagrid({ queryParams:{status:-1}});
	},
	addBar:function(){//显示添加框
		$('#booksInfoAddDialog').dialog('open').dialog('setTitle','添加');
		$('#booksInfoAddForm').form('clear');
		$('#unit1').combobox('select',0);
		$('#unit2').combobox('select',0);
		$('#current_type').combobox('select',0);
		$('#countrylist').combobox('select',0);
		$('#manua_id').textbox('setValue',manua_id);
	},
	editBar:function(){//显示编辑框
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
        if (booksInfoRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(booksInfoRow.length==1){            	
        	$('#booksInfoEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#booksInfoEditForm').form('load',booksInfoRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
        if (booksInfoRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+booksInfoRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<booksInfoRow.length;i++){
                		ids.push(booksInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(booksInfoDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#booksInfoAddDialog').dialog('close');       
								$('#booksInfoDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	clearBar:function(){//删除/清空回收站数据
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
        if (booksInfoRow.length>0){
        	$.messager.confirm('清空提示','真的要清空这'+booksInfoRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<booksInfoRow.length;i++){
                		ids.push(booksInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(booksInfoClearUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#booksInfoAddDialog').dialog('close');       
								$('#booksInfoDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	undoBar:function(){//恢复回收站数据
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
        if (booksInfoRow.length>0){
        	$.messager.confirm('恢复提示','真的要恢复这'+booksInfoRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<booksInfoRow.length;i++){
                		ids.push(booksInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(booksInfoUndoUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#booksInfoAddDialog').dialog('close');       
								$('#booksInfoDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	examineBar:function(value){//审核数据
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
		if(value==1){
			var text='确定审核通过吗！';
		}
		if(value==2){
			var text='确定审核不通过吗！';
		}
        if (booksInfoRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<booksInfoRow.length;i++){
                		ids.push(booksInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(booksInfoExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#booksInfoDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	addInboundBar:function(){//添加入库
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
		var text='确定添加入库数据吗！';
        if (booksInfoRow.length>0){
        	$.messager.confirm('添加提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<booksInfoRow.length;i++){
                		ids.push(booksInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(otherInboundAddUrl,{id:id,otherInboundCate:otherInboundCate},function(result){
                        if (result.status){
							$.messager.alert('提示',result.message);
							$('#Tabs').tabs('close','账册详情');
							$('#otherInboundInfoDatagrid').datagrid('reload'); 
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	pushBar:function(){
		var Row=$('#booksInfoDatagrid').datagrid('getSelections');
		var text='确定推送吗！';

        if (Row.length>0){
        	$.messager.confirm('推送提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<Row.length;i++){
						if(Row[i].status=='未审核'){
							$.messager.alert('推送提示','未审核不能推送','error');
							return false;
						}
                		ids.push(Row[i].id);
						// if(Row[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+Row[i].goods_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(pushinfoOrderUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('推送成功',result.info);
						}else{
							$.messager.alert('错误提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}